*****************************************************************
*																*
*				Title: 5_spatial_regressions		 	 	   	*		
*															    *
*****************************************************************

/*  ================================  NOTES  ================================

+ PURPSOSE:
	
	In this do-file we create Figure 5
	
		 
		
+ Inputs:

		* 1. panel_cotton
			 
			 
		* 2. panel_metallurgy
			
			 
		* 3. panel_paper


==============================  TOP MATTER ==============================*/

************** END HEADER **************

	*set paths
	global proc_data "`pdir'/data"
	global output "`pdir'/outputs/main_paper"

********************************** END HEADER **********************************


**** The code below creates Figure 5 
**** Effect of distance to high productivity firms

*** 1. Standardize variables
*** 2. Run regressions by industry-year spearately
*** 3. Plot standardized coefficients

estimates clear

********************************** 1. Cotton *************************************************************


use "$proc_data/panel_cotton.dta", clear


			** 1806
			cap drop c_logYoverL_real
			cap drop c_lndistance_above_p90_1800
			reg logYoverL_real lndistance_above_p90_1800 i.id_dept if year == 1806, beta r
			center logYoverL_real if e(sample), standardize
			center lndistance_above_p90_1800 if e(sample), standardize
			label var c_lndistance_above_p90_1800 "lnDist to p90 (1800)"
			eststo: reg c_logYoverL_real c_lndistance_above_p90_1800 i.id_dept if year == 1806, cluster(id_dept)
			est store cotton_1806_FE
			
	
			** 1840
			cap drop c_logYoverL_real
			cap drop c_lndistance_above_p90_1840
			reg logYoverL_real lndistance_above_p90_1840 i.id_dept if year == 1840, beta r
			center logYoverL_real if e(sample), standardize
			center lndistance_above_p90_1840 if e(sample), standardize
			label var c_lndistance_above_p90_1840 "lnDist to p90 (1840)"
			eststo: reg c_logYoverL_real c_lndistance_above_p90_1840 i.id_dept if year == 1840, cluster(id_dept)
			est store cotton_1840_FE
			


***********************************2. Metallurgy************************************************************


use "$proc_data/panel_metallurgy.dta", clear

					
			** 1811
			cap drop c_logYmeanoverL_real
			cap drop c_lndistance_above_p90_1800
			reg logYmeanoverL_real lndistance_above_p90_1800 i.id_dept  if year == 1811, beta 
			center logYmeanoverL_real if e(sample), standardize
			center lndistance_above_p90_1800 if e(sample), standardize
			label var c_lndistance_above_p90_1800 "lnDist to p90 (1800)"
			eststo: reg c_logYmeanoverL_real c_lndistance_above_p90_1800 i.id_dept if year == 1811, cluster(id_dept)
			est store metallurgy_1811_FE
					 
		
			** 1840
			cap drop c_logYmeanoverL_real
			cap drop c_lndistance_above_p90_1840
			reg logYmeanoverL_real lndistance_above_p90_1840 i.id_dept  if year == 1840, beta r
			center logYmeanoverL_real if e(sample), standardize
			center lndistance_above_p90_1840 if e(sample), standardize
			label var c_lndistance_above_p90_1840 "lnDist to p90 (1840)"
			eststo: reg c_logYmeanoverL_real c_lndistance_above_p90_1840 i.id_dept  if year == 1840, cluster(id_dept)
			est store metallurgy_1840_FE
			
		


************************************3. Paper milling******************************************************************



use "$proc_data/panel_paper.dta", clear

	
			** 1794
			cap drop c_logYoverL_real 
			cap drop c_lndistance_above_p90_1800
			reg logYoverL_real lndistance_above_p90_1800 i.id_dept  if year == 1794, beta r
			center logYoverL_real if e(sample), standardize
			center lndistance_above_p90_1800 if e(sample), standardize
			label var c_lndistance_above_p90_1800 "lnDist to p90 (1800)"
			eststo: reg c_logYoverL_real c_lndistance_above_p90_1800 i.id_dept if year == 1794, cluster(id_dept)
			est store paper_1794_FE

			
			** 1840
			cap drop c_logYoverL_real 
			cap drop c_lndistance_above_p90_1840
			reg logYoverL_real lndistance_above_p90_1840  i.id_dept if year == 1840, beta r
			center logYoverL_real if e(sample), standardize
			center lndistance_above_p90_1840 if e(sample), standardize
			label var c_lndistance_above_p90_1840 "lnDist to p90 (1840)"
			eststo: reg c_logYoverL_real c_lndistance_above_p90_1840  i.id_dept if year == 1840, cluster(id_dept)
			est store paper_1840_FE


************	4. Plot standardized coeffs	***********

*4.1 Cotton		

	coefplot (cotton_1806_FE, keep(c_lndistance_above_p90_1800) barwidth(0.5) col(gs13) ciopts(col(gs6) recast(rcap))   citop  aseq("1800")) ///
		(cotton_1840_FE, keep(c_lndistance_above_p90_1840) barwidth(0.5)  col(gs13) ciopts(col(gs6) recast(rcap))   citop aseq("1840")) ///			
	, scheme(s1color) vert levels(90) leg(off)  swapnames recast(bar)  yscale(range(0.2 (0.4) -1.2))  ylabel(0 -0.5  -1, labsize(medlarge)) xlabel(, labsize(medlarge))   
	graph export "$output/Figures/Figure_5A.eps", replace
	graph export "$output/Figures/Figure_5A.pdf", replace	

	
*4.2 Metallurgy 

	coefplot (metallurgy_1811_FE, keep(c_lndistance_above_p90_1800) barwidth(0.5) col(gs13) ciopts(col(gs6) recast(rcap))   citop  aseq("1800")) ///
		(metallurgy_1840_FE, keep(c_lndistance_above_p90_1840) barwidth(0.5)  col(gs13) ciopts(col(gs6) recast(rcap))   citop aseq("1840")) ///			
	, scheme(s1color) vert levels(90) leg(off)  swapnames recast(bar)  yscale(range(0.2 (0.4) -1.2))  ylabel(0 -0.5  -1, labsize(medlarge)) xlabel(, labsize(medlarge)) 
		graph export "$output/Figures/Figure_5B.eps", replace
		graph export "$output/Figures/Figure_5B.pdf", replace

*4.3 Paper 
	
	coefplot (paper_1794_FE, keep(c_lndistance_above_p90_1800) barwidth(0.5) col(gs13) ciopts(col(gs6) recast(rcap))   citop  aseq("1800")) ///
		(paper_1840_FE, keep(c_lndistance_above_p90_1840) barwidth(0.5)  col(gs13) ciopts(col(gs6) recast(rcap))   citop aseq("1840")) ///			
	, scheme(s1color) vert levels(90) leg(off)  swapnames recast(bar)  yscale(range(0.2 (0.4) -1.2)) ylabel(0 -0.5  -1, labsize(medlarge)) xlabel(, labsize(medlarge))  
		graph export "$output/Figures/Figure_5C.eps", replace
		graph export "$output/Figures/Figure_5C.pdf", replace			
	
		

